問題の説明
Python の super() と super (className,self) の違い (Difference between super() and super (className,self) in Python)
次のようにコードを切り取りました:
class A:
def test(self):
return 'A'
class B(A):
def test(self):
return 'B‑>' + super(B, self).test()
print(B().test())
出力: B‑>A
このように書くと、同じ出力が得られます:
class A:
def test(self):
return 'A'
class B(A):
def test(self):
return 'B‑>' + super().test() # change to super()
print(B().test())
どちらの場合も、同じ出力が得られます。では、この 2 種類の super
の呼び出しの違いは何ですか? どちらを使用する場合の長所と短所は何ですか?
リファレンスソリューション
方法 1:
In Python 2, only the super(className,self)
syntax was possible. Since It was the most used version, as of Python 3
providing no arguments will act the same.
There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable
(by Taohidul Islam、FrenchMasterSword)